package cn.oesoft.tennissystem.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;

import cn.oesoft.tennissystem.dao.TennisRacketMapper;
import cn.oesoft.tennissystem.dao.CommentMapper;
import cn.oesoft.tennissystem.dao.StatisticMapper;
import cn.oesoft.tennissystem.model.entity.TennisRacket;
import cn.oesoft.tennissystem.model.entity.Comment;
import cn.oesoft.tennissystem.model.entity.Statistic;
import cn.oesoft.tennissystem.model.entity.TennisRacket;
import cn.oesoft.tennissystem.model.responseData.StaticticsBo;
import cn.oesoft.tennissystem.service.ISiteService;

/**
 * @Classname SiteServiceImpl
 * @Description TODO
 * @Date 2019-3-14 10:15
 * @Created by CrazyStone
 */
@Service
@Transactional
public class SiteServiceImpl implements ISiteService {
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private TennisRacketMapper tennisRacketMapper;
    @Autowired
    private StatisticMapper statisticMapper;

    /**
     * 用户点击一次某个球拍，该球拍的点击量就要自增1
     */
    @Override
    public void updateStatistics(TennisRacket tennisRacket) {
    	//获取某个球拍的统计数据
    	Statistic statistic = statisticMapper.getByTennisRacketId(tennisRacket.getId());
        //该球拍的点击量自增1
    	statistic.setHits(statistic.getHits()+1);
    	//更新该球拍的点击量
        statisticMapper.updateHitsByTennisRacketId(statistic);
    }

    @Override
    public List<Comment> recentComments(int limit) {
        PageHelper.startPage(1, limit>10 || limit<1 ? 10:limit);
        List<Comment> byPage = commentMapper.selectNewComment();
        return byPage;
    }

    @Override
    public List<TennisRacket> recentTennisRackets(int limit) {
        PageHelper.startPage(1, limit>10 || limit<1 ? 10:limit);
        //List<TennisRacket> list = tennisRacketMapper.selectTennisRacketWithPage();
        List<TennisRacket> list = tennisRacketMapper.getWithPage();
        // 封装文章统计数据
        for (int i = 0; i < list.size(); i++) {
            TennisRacket tennisRacket = list.get(i);
            //Statistic statistic = statisticMapper.selectStatisticWithTennisRacketId(tennisRacket.getId());
            Statistic statistic = statisticMapper.getByTennisRacketId(tennisRacket.getId());
            tennisRacket.setHits(statistic.getHits());
            tennisRacket.setCommentsNum(statistic.getCommentsNum());
        }
        return list;
    }
    /**
     * 获取全站统计数据，包括：
     * 球拍总数、评论总数
     */
    @Override
    public StaticticsBo getStatistics() {
        StaticticsBo staticticsBo = new StaticticsBo();
//        Integer tennisRackets = tennisRacketMapper.countTennisRacket();
        //球拍总数
        Integer tennisRacketTotalNum = tennisRacketMapper.count();
        //评论总数
        Integer commentTotalNum = commentMapper.countComment();
        staticticsBo.setTennisRacketTotalNum(tennisRacketTotalNum);
        staticticsBo.setCommentTotalNum(commentTotalNum);
        return staticticsBo;
    }
}

